using Gltf;
using UnityEngine;

public class CesiumTextureUtility
{

    public static Texture2D createimage()
    {
        Texture2D tex = new Texture2D(256, 256);
        Color[] colors = new Color[256 * 256];
        for (int i = 0; i < (256 * 256); i++)
        {
            colors[i] = new Color(Random.value, Random.value, Random.value, 1);
        }
        tex.SetPixels(0, 0, 256, 256, colors);
        tex.Apply();
        return tex;
    }
    public static void saveImage(string name, Texture2D image)
    {
        var imbyte = image.EncodeToJPG();
        System.IO.File.WriteAllBytes(name, imbyte);
    }
    public static void saveImage(string name, ImageCesium image)
    {
        TextureFormat pixelFormat;
        switch (image.channels)
        {
            case 1:
                pixelFormat = TextureFormat.Alpha8;
                break;
            case 2:
                pixelFormat = TextureFormat.RG16;
                break;
            case 3:
            case 4:
            default:
                pixelFormat = TextureFormat.RGBA32;
                break;
        };
        Texture2D pResult = new Texture2D(image.width, image.height, pixelFormat, false);
        pResult.SetPixelData(image.pixelData, 0);

        var imbyte = pResult.EncodeToJPG();
        System.IO.File.WriteAllBytes(name, imbyte);
    }
    public static object loadTextureAnyThreadPart(ImageCesium image,
    TextureWrapMode addressX = TextureWrapMode.Clamp,
    TextureWrapMode addressY = TextureWrapMode.Clamp,
     FilterMode filter = FilterMode.Bilinear)
    {
        TextureFormat pixelFormat;
        switch (image.channels)
        {
            case 1:
                pixelFormat = TextureFormat.Alpha8;
                break;
            case 2:
                pixelFormat = TextureFormat.RG16;
                break;
            case 3:
            case 4:
            default:
                pixelFormat = TextureFormat.RGBA32;
                break;
        };

        Texture2D pResult = new Texture2D(image.width, image.height, pixelFormat, false);

        pResult.wrapModeU = addressX;
        pResult.wrapModeV = addressY;
        pResult.filterMode = filter;

        pResult.SetPixelData(image.pixelData, 0);
        pResult.Apply();
        // if (pResult->filter == TextureFilter::TF_Trilinear)
        // {
        //     // Generate mip levels.
        //     // TODO: do this on the GPU?
        //     int32_t width = image.width;
        //     int32_t height = image.height;
        //     int32_t channels = image.channels;

        //     while (width > 1 || height > 1)
        //     {
        //         FTexture2DMipMap* pLevel = new FTexture2DMipMap();
        //         pResult->pTextureData->Mips.Add(pLevel);

        //         pLevel->SizeX = width >> 1;
        //         if (pLevel->SizeX < 1)
        //             pLevel->SizeX = 1;
        //         pLevel->SizeY = height >> 1;
        //         if (pLevel->SizeY < 1)
        //             pLevel->SizeY = 1;

        //         pLevel->BulkData.Lock(LOCK_READ_WRITE);

        //         void* pMipData =
        //             pLevel->BulkData.Realloc(pLevel->SizeX * pLevel->SizeY * channels);

        //         // TODO: Premultiplied alpha? Cases with more than one byte per channel?
        //         // Non-normalzied pixel formats?
        //         if (!stbir_resize_uint8(
        //                 static_cast <const unsigned char*> (pTextureData),
        //       width,
        //       height,
        //       0,
        //       static_cast < unsigned char*> (pMipData),
        //       pLevel->SizeX,
        //       pLevel->SizeY,
        //       0,
        //       channels)) {
        //             // Failed to generate mip level, use bilinear filtering instead.
        //             pResult->filter = TextureFilter::TF_Bilinear;
        //             for (int32_t i = 1; i < pResult->pTextureData->Mips.Num(); ++i)
        //             {
        //                 pResult->pTextureData->Mips[i].BulkData.Unlock();
        //             }
        //             pResult->pTextureData->Mips.RemoveAt(
        //                 1,
        //                 pResult->pTextureData->Mips.Num() - 1);
        //             break;
        //         }

        //         width = pLevel->SizeX;
        //         height = pLevel->SizeY;
        //         pTextureData = pMipData;
        //     }
        // }

        // // Unlock all levels
        // for (int32_t i = 0; i < pResult->pTextureData->Mips.Num(); ++i)
        // {
        //     pResult->pTextureData->Mips[i].BulkData.Unlock();
        // }

        return pResult;
    }

}